package springboot1706.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import springboot1706.bean.User;
import springboot1706.service.UserServiceImpl;



@Controller
@RequestMapping("/userInfo")
public class UserController {
	Logger log = LoggerFactory.getLogger(TestController.class);
	@Autowired
	private UserServiceImpl userServiceImpl;
	
	/**
	 * 注册
	 * @return
	 */
	@RequestMapping("/register")
	public String register(){
		return "register";
	}
             
	
	
	@RequestMapping("/reg")
	public String reg(HttpServletRequest request){
		User user = new User();
		user.setUserName(request.getParameter("user"));
		user.setPassWord(request.getParameter("psw"));
		user.setCellphone(request.getParameter("phone"));
		int result = userServiceImpl.insert(user);
		if (result == 1) {
			request.setAttribute("success", "注册成功");
			request.getSession().setAttribute("username",user.getUserName());
			return "redirect:/userInfo/login";
		} else {
			request.setAttribute("error", "●︿●用户名已存在，请重新注册！");
			return "register";
		}
	}
	
	/**
	 * 登录
	 * @return
	 */
	
	@RequestMapping("/login")
	public String login(){
		return "login";
	}
	
	
	@RequestMapping("dologin")
	public String login(HttpServletRequest request){
		User user = new User();
		user.setUserName(request.getParameter("user"));
		user.setPassWord(request.getParameter("psw"));
		User u = userServiceImpl.findUser(user);
		if (u == null) {
			request.setAttribute("error","用户名或密码错误");
			return "login";
		} else {
			request.getSession().setAttribute("username",user.getUserName());
			return "index";
		}
	}
	
	
	/**
	 * 后台登录
	 * @return
	 */
	@RequestMapping("/adminlogin")
	public String adminlogin(HttpServletRequest request,HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse,Model model){
		 String captchaId = (String) request.getSession().getAttribute("vrifyCode");   
         String parameter = request.getParameter("vrifyCode");  
         System.out.println("Session  vrifyCode "+captchaId+" form vrifyCode "+parameter);
         User user = new User();
  		user.setUserName(request.getParameter("user"));
  		user.setPassWord(request.getParameter("psw"));
  		User u = userServiceImpl.findUser(user);
  			if (u==null || !captchaId.equals(parameter)) {  
  	        	model.addAttribute("user", "错误的验证码");  
  	        	return "admin/adminlogin";
  	        
  		}else {  
         	model.addAttribute("user", "正确的验证码"); 
         	return "admin/index";
         }  
	}

	
	
	/**
	 * 退出
	 * @return
	 */
	@RequestMapping("/exit")
	public String exit(HttpServletRequest request){
		request.getSession().removeAttribute("username");
//		response.sendRedirect(request.getHeader("Referer"));
		return "index";
	}
	
	
	/**
	 * 首页
	 * @return
	 */
	@RequestMapping("/index")
	public String index(){
		return "index";
	}
	
	
}
